home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-03 / qbasicpg.zip / REPEAT.BAS < prev    next >
BASIC Source File  |  1989-08-31  |  3KB  |  101 lines

  1. ' REPEAT.BAS
  2. ' This program prompts the user for a set number of lines and a search
  3. '   string and then prints the lines and the number of matches found.
  4.  
  5. ' set maximum number of lines that can be entered and declare string
  6. '   array to hold lines
  7.  
  8. CONST MAXLINES% = 10
  9. DIM inputLines$(MAXLINES%)
  10.  
  11. ' declare GetText subprogram and Repeat% function
  12.  
  13. DECLARE SUB GetText (inputLines$(), numOfLines%)
  14. DECLARE FUNCTION Repeat% (search$, base$)
  15.  
  16. CLS
  17.  
  18. ' call GetText subprogram to get input from user; at return the
  19. '   numOfLines% variable will contain number of lines received
  20.  
  21. GetText inputLines$(), numOfLines%
  22.  
  23. ' get pattern to be searched for from user
  24.  
  25. PRINT
  26. INPUT "Enter the string to be searched for:  ", pattern$
  27. PRINT
  28.  
  29. ' call Repeat% function to determine the number of matches per line;
  30. '   totalRepeats% variable tracks number of total matches
  31.  
  32. FOR i = 1 TO numOfLines%
  33.     lineRepeats% = Repeat%(pattern$, inputLines$(i))
  34.     totalRepeats% = totalRepeats% + lineRepeats%
  35. NEXT i
  36.  
  37. ' display lines entered by the user...
  38.  
  39. PRINT "You entered the following lines:"
  40. PRINT
  41. FOR i = 1 TO numOfLines%
  42.     PRINT inputLines$(i)
  43. NEXT i
  44.  
  45. ' and the total number of matches
  46.  
  47. PRINT
  48. PRINT "The pattern '"; pattern$; "' appears"; totalRepeats%; "times"
  49.  
  50. END
  51.  
  52. SUB GetText (strArray$(), count%)
  53.  
  54. ' The GetText subprogram fills the strArray$() array with text
  55. '   entered at the keyboard.  The number of lines that can be
  56. '   entered is determined by the global constant MAXLINES%.
  57. '   Both strArray$() and count% (the number of lines actually
  58. '   entered) are returned to the main program.
  59.  
  60. PRINT "Enter up to"; MAXLINES%; "lines of text; to end, ";
  61. PRINT "press Enter on a new line."
  62. PRINT
  63. count% = 0
  64.    
  65. DO
  66.     LINE INPUT "-> "; inLine$  ' get line from user
  67.     IF (inLine$ <> "") THEN    ' if line is not blank, copy it
  68.         count% = count% + 1    '   to the strArray$() array
  69.         strArray$(count%) = inLine$
  70.     END IF
  71. ' loop until count% = MAXLINES% or an empty line is received
  72. LOOP WHILE (count% < MAXLINES%) AND (inLine$ <> "")
  73.  
  74. END SUB
  75.  
  76. FUNCTION Repeat% (search$, base$)
  77.  
  78. ' The Repeat% function returns the number of times a search string
  79. '   is found in a base string.
  80.  
  81. searchLength% = LEN(search$)  ' determine length of search string
  82. baseLength% = LEN(base$)      ' determine length of base string
  83. currentChar% = 1              ' character offset in base string
  84. num% = 0                      ' running total of matches found in base
  85.  
  86. ' loop until entire string is processed or INSTR returns a 0
  87.  
  88. WHILE (currentChar% <= baseLength%) AND (currentChar% <> 0)
  89.     currentChar% = INSTR(currentChar%, base$, search$)
  90.     IF (currentChar% <> 0) THEN  ' if not zero, a match was found
  91.         num% = num% + 1          ' increment number of matches
  92.         ' new offset equals current offset plus search-string length
  93.         currentChar% = currentChar% + searchLength%
  94.     END IF
  95. WEND
  96.  
  97. Repeat% = num%                ' return total number of matches
  98.  
  99. END FUNCTION
  100.  
  101.